/**************************************************************************
 * @name: Menu.c
 * @brief: 菜单界面
 * @date: 2025/01/15
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include "main.h"
#include "bmp.h"
#include "menu.h"
#include "keyboard.h"
#include "user.h"
#include "start.h"
static double charge_kwh = 0.0;        // 充电数值
static double charge_price = 0.0;      // 充电价格
time_t charge_start_time;              // 开始充电时间
char charge_kwh_str[32];               // 用于存储 charge_kwh 转换后的字符串
char charge_price_str[32];             // 用于存储 charge_price 转换后的字符串
char charge_time_str[32];              // 用于存储 charge_time 转换后的字符串
char charge_rate[32];                  // 用于存储 电量占比 的字符串
char login_user_balance[16];           // 用于存储 当前用户余额的字符串
static bool is_charging = false;       // 充电状态
static bool is_thread_running = false; // 控制线程的运行状态
static pthread_t charge_thread;        // 子线程句柄

void Initialize_Random()
{
    srand(time(NULL)); // 初始化随机数种子
}

// 随机选择一个二维码文件
const char *Get_Random_QR_Code()
{
    int qr_code_count = 5;
    int random_index = rand() % qr_code_count + 1; // 生成1到5之间的随机数
    static char qr_code_path[50];
    snprintf(qr_code_path, sizeof(qr_code_path), "/tmp/starge_project/Data/qrCode/%03d.bmp", random_index);
    return qr_code_path;
}

/**************************************************************************
 * @name: Menu_Change
 * @brief: 每次切换页面就重置触摸板的坐标
 * @param {unsigned int} page：当前跳转的页面
 * @note: None
 * @retval: None
 **************************************************************************/
void Menu_Change(unsigned int page)
{
    touch_data.page = page;
    touch_data.ts_x = 0;
    touch_data.ts_y = 0;
}

// 判断触摸位置是否在指定区域内
int Is_Touch_In_Area(int x_min, int x_max, int y_min, int y_max)
{
    return (touch_data.ts_x >= x_min && touch_data.ts_x <= x_max &&
            touch_data.ts_y >= y_min && touch_data.ts_y <= y_max);
}

/**************************************************************************
 * @name: Show_UserInfo
 * @brief: 展示用户信息和当前时间
 * @note: None
 * @retval: None
 **************************************************************************/
static void Show_UserInfo(void)
{
    if (!(touch_data.page == FAST_CHARGE_PAGE || touch_data.page == SLOW_CHARGE_PAGE))
    {
        Show_Font(25, 50, 25, 4, getColor(0, 128, 89, 60), 0, 0, "返回", getColor(0, 255, 255, 255), 50, 35, 10);
    }
    if (login_user != NULL)
    {
        sprintf(login_user_balance, "余额: %.2f", login_user->Balance);
        Show_Font(25, 110, 25, 4, getColor(0, 128, 89, 60), 0, 0, login_user->Account, getColor(0, 255, 255, 255), 110, 80, 10);
        Show_Font(25, 110, 25, 4, getColor(0, 128, 89, 60), 0, 0, login_user_balance, getColor(0, 255, 255, 255), 110, 210, 10);
    }
    Show_Font(25, 200, 25, 4, getColor(0, 128, 89, 60), 0, 0, cur_datetime, getColor(0, 255, 255, 255), 200, 600, 10);
}

// 启动充电计时
void Start_Charging_Timer()
{
    charge_start_time = time(NULL); // 记录充电开始时间
}

// 获取并显示经过的时间
void Elapsed_Charging_Time()
{
    time_t current_time = time(NULL);
    double seconds_elapsed = difftime(current_time, charge_start_time);

    // 计算小时、分钟和秒
    int hours = (int)(seconds_elapsed / 3600);
    int minutes = ((int)seconds_elapsed % 3600) / 60;
    int seconds = (int)seconds_elapsed % 60;
    sprintf(charge_time_str, "充电时长: %02d:%02d:%02d\n", hours, minutes, seconds);
}

// 更新充电数值
void Update_Charge_Value(void)
{
    // 计算充电价格
    if (touch_data.page == FAST_CHARGE_PAGE)
    {
        // 计算每秒增加的电量
        double charge_increment_kwh = CHARGE_POWER_KW(FAST_CHARGE_V, FAST_CHARGE_A) / SECONDS_PER_HOUR;

        // 更新充电量
        charge_kwh += charge_increment_kwh;

        // 确保充电量不超过电池容量
        if (charge_kwh > BATTERY_KWH)
        {
            charge_kwh = BATTERY_KWH;
            is_charging = false;
        }
        charge_price = charge_kwh * FAST_CHARGE_PRICE; // 快充价格
    }
    else if (touch_data.page == SLOW_CHARGE_PAGE)
    {
        // 计算每秒增加的电量
        double charge_increment_kwh = CHARGE_POWER_KW(SLOW_CHARGE_V, SLOW_CHARGE_A) / SECONDS_PER_HOUR;
        // 更新充电量
        charge_kwh += charge_increment_kwh;
        // 确保充电量不超过电池容量
        if (charge_kwh > BATTERY_KWH)
        {
            charge_kwh = BATTERY_KWH;
            is_charging = false;
        }
        charge_price = charge_kwh * SLOW_CHARGE_PRICE; // 慢充价格
    }
    Elapsed_Charging_Time();
    // 将 double 转换为字符串
    sprintf(charge_kwh_str, "%.2f", charge_kwh); // 保留两位小数
    sprintf(charge_price_str, "%.2f", charge_price);
    sprintf(charge_rate, "%.2f%%", charge_kwh / BATTERY_KWH * 100);
}

// 显示充电动画和数值
void *Charge_Animation_Thread(void *arg)
{
    while (is_thread_running) // 线程运行控制
    {
        for (;;)
        {
            if (is_charging)
            {
                // 显示充电动画的 BMP 图像
                Start_Show(CHARGE_DIR_PATH, 630, 130);
                Update_Charge_Value();
                sleep(1);
            }
            else
                break;
        }
    }
    return NULL;
}

// 注册登录选择界面
void Goto_Money_Page(void)
{
    // for (;;)
    // {
    Menu_Change(MONEY_PAGE);
    printf("当前消费的金额是:%.2f\n", charge_price);
    double money = 0.0;
    char *money_str = (char *)calloc(1, 16); // 最多支持7位字符
    Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
    Bmp_Show(KEY_BOARD, lcd_buf, 50, 30);
    Show_Font(50, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "充值", getColor(0, 255, 255, 255), 100, 400, 10);
    Show_Font(25, 150, 50, 4, getColor(0, 128, 89, 60), 0, 0, "请选择预充金额", getColor(0, 255, 255, 255), 150, 400, 70);
    Show_Font(25, 100, 50, 4, getColor(0, 255, 158, 60), 35, 12, "30", getColor(0, 255, 255, 255), 100, 400, 100);
    Show_Font(25, 100, 50, 4, getColor(0, 255, 158, 60), 35, 12, "60", getColor(0, 255, 255, 255), 100, 530, 100);
    Show_Font(25, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "金额", getColor(0, 255, 255, 255), 100, 400, 200);
    Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, money_str, getColor(0, 0, 0, 0), 350, 400, 230);
    Lcd_Refresh(lcd_mp, lcd_buf);
    while (touch_data.page == MONEY_PAGE)
    {
        Show_UserInfo();
        if (Is_Touch_In_Area(400, 500, 100, 160)) // 30
        {
            strcpy(money_str, "30");
        }
        if (Is_Touch_In_Area(530, 630, 100, 160)) // 60
        {
            strcpy(money_str, "60");
        }
        if (Is_Touch_In_Area(400, 750, 220, 280)) // 点击了金额输入框
        {
            Keyboard_Event(money_str, NULL, false, 16, 400, 230);
        }
        // 更新内容到屏幕（保持显示）
        Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, money_str, getColor(0, 0, 0, 0), 350, 400, 230);
        Lcd_Refresh(lcd_mp, lcd_buf);

        if (Is_Touch_In_Area(250, 350, 360, 480)) // 点击了提交按钮
        {
            // 校验账号和密码
            int valid = 1; // 标记是否有效
            if (strlen(money_str) <= 0)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充值金额不能为空", getColor(0, 0, 0, 255), 350, 400, 300);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 150);
            }
            sscanf(money_str, "%lf", &money);
            if (money - charge_price < 0)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充值的金额不够扣除消费金额", getColor(0, 0, 0, 255), 350, 400, 300);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 280);
            }

            // 如果账号和密码校验都通过，则显示弹窗
            if (valid)
            {
                printf("money:%.2f, charge_price:%.2f,Add:%.2f\n", money, charge_price, money - charge_price);
                User_Credit_Add(money - charge_price);
                free(money_str);
                // 绘制弹窗
                Show_Info_Popup("充值成功！", "确认");
                Menu_Change(MAIN_PAGE);
                return;
            }
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            if (charge_price > 0)
            {
                Show_Info_Popup("不给钱？", "确认");
                return;
            }
            Menu_Change(MAIN_PAGE);
            return;
        }
    }
    // }
}

// 快充界面
void Goto_Fast_Charge(void)
{
    int btn = 0;              // 0为默认展示充电监控，1为展示设备信息
    is_thread_running = true; // 启动线程运行
    is_charging = true;       // 默认开启充电动画
    Start_Charging_Timer();   // 记录开始充电时间
    Menu_Change(FAST_CHARGE_PAGE);
    // 创建充电动画子线程
    if (pthread_create(&charge_thread, NULL, Charge_Animation_Thread, NULL) != 0)
    {
        fprintf(stderr, "无法创建充电动画线程\n");
        return;
    }
    while (touch_data.page == FAST_CHARGE_PAGE)
    {
        Bmp_Show(CHARGE_BMP, lcd_buf, 0, 0);
        Bmp_Show(CHARGE_FIRST_BMP, lcd_buf, 630, 130);
        Show_UserInfo();
        Show_Font(25, 80, 25, 4, getColor(0, 128, 89, 60), 0, 0, charge_rate, getColor(0, 41, 250, 26), 80, 650, 140);
        Show_Font(18, 110, 18, 4, getColor(0, 128, 89, 60), 0, 0, charge_time_str, getColor(0, 41, 250, 26), 110, 620, 110);
        // 显示充电动画的 BMP 图像
        if (Is_Touch_In_Area(80, 135, 150, 230) || btn == 0) // 充电监控
        {
            btn = 0;
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电量: ", getColor(0, 41, 250, 26), 80, 250, 150);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, charge_kwh_str, getColor(0, 255, 255, 255), 80, 250, 180);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "kwh", getColor(0, 255, 255, 255), 50, 330, 180);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电费用: ", getColor(0, 41, 250, 26), 80, 460, 150);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, charge_price_str, getColor(0, 255, 255, 255), 80, 460, 180);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "元", getColor(0, 255, 255, 255), 50, 540, 180);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电流: ", getColor(0, 41, 250, 26), 80, 250, 300);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, FAST_CHARGE_AUS, getColor(0, 255, 255, 255), 80, 250, 330);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "A", getColor(0, 255, 255, 255), 50, 330, 330);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电压: ", getColor(0, 41, 250, 26), 80, 460, 300);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, FAST_CHARGE_VUS, getColor(0, 255, 255, 255), 80, 460, 330);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "V", getColor(0, 255, 255, 255), 50, 540, 330);
        }
        if (Is_Touch_In_Area(80, 135, 270, 350) || btn == 1) // 设备信息
        {
            btn = 1;
            Show_Font(22, 100, 22, 4, getColor(0, 128, 89, 60), 0, 0, "粤嵌快充站", getColor(0, 41, 250, 26), 100, 215, 120);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 0, "车位: ", getColor(0, 41, 250, 26), 50, 215, 160);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 0, "10010", getColor(0, 255, 255, 255), 50, 265, 160);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电压: ", getColor(0, 41, 250, 26), 80, 215, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, FAST_CHARGE_VS, getColor(0, 255, 255, 255), 80, 290, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电流: ", getColor(0, 41, 250, 26), 80, 400, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, FAST_CHARGE_AS, getColor(0, 255, 255, 255), 80, 475, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "功       率: ", getColor(0, 41, 250, 26), 80, 215, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, FAST_CHARGE_KW, getColor(0, 255, 255, 255), 80, 290, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "当前电价: ", getColor(0, 41, 250, 26), 80, 400, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "0.6元", getColor(0, 255, 255, 255), 80, 475, 240);
        }
        if (Is_Touch_In_Area(600, 740, 350, 420)) // 充电开关
        {
            is_charging = !is_charging;
            touch_data.ts_x = 0;
            touch_data.ts_y = 0;
            int ret = Show_Choose_Popup("请选择支付方式", "扫码付费", "余额付费");
            if (ret == 1) // 扫码付费
            {
                const char *qr_code_path = Get_Random_QR_Code();
                if (Show_Img_Popup(qr_code_path, "我已支付"))
                {
                    // 页面退出时终止线程
                    is_thread_running = false;         // 停止线程运行
                    pthread_join(charge_thread, NULL); // 等待线程退出
                    Menu_Change(MAIN_PAGE);
                    return;
                }
                else
                    is_charging = !is_charging; // 退出支付则继续充电
            }
            else if (ret == 2) // 余额付费
            {
                if (User_Credit_Sub(charge_price))
                {
                    // 页面退出时终止线程
                    is_thread_running = false;         // 停止线程运行
                    pthread_join(charge_thread, NULL); // 等待线程退出
                    Menu_Change(MAIN_PAGE);
                    return;
                }
                else
                {
                    // 如果返回false，则代表用户余额不够扣除当前费用，则跳转到充值模块，充值后直接扣除费用，并把剩下的余额存到账户中
                    int ret = Show_Choose_Popup("您的余额不足,请充值", "", "确定");
                    if (ret)
                    {
                        // 页面退出时终止线程
                        is_thread_running = false;         // 停止线程运行
                        pthread_join(charge_thread, NULL); // 等待线程退出
                        Menu_Change(MONEY_PAGE);
                        return;
                    }
                }
            }
            printf("充电开关:%d\n", is_charging);
        }
        Lcd_Refresh(lcd_mp, lcd_buf);
    }
    // 页面退出时终止线程
    is_thread_running = false;         // 停止线程运行
    pthread_join(charge_thread, NULL); // 等待线程退出
}

// 慢充界面
void Goto_Slow_Charge(void)
{
    int btn = 0;              // 0为默认展示充电监控，1为展示设备信息
    is_thread_running = true; // 启动线程运行
    is_charging = true;       // 默认开启充电动画
    Start_Charging_Timer();   // 记录开始充电时间
    Menu_Change(SLOW_CHARGE_PAGE);
    // 创建充电动画子线程
    if (pthread_create(&charge_thread, NULL, Charge_Animation_Thread, NULL) != 0)
    {
        fprintf(stderr, "无法创建充电动画线程\n");
        return;
    }
    while (touch_data.page == SLOW_CHARGE_PAGE)
    {
        Bmp_Show(CHARGE_BMP, lcd_buf, 0, 0);
        Bmp_Show(CHARGE_FIRST_BMP, lcd_buf, 630, 130);
        Show_UserInfo();
        Show_Font(25, 80, 50, 4, getColor(0, 128, 89, 60), 0, 0, charge_rate, getColor(0, 41, 250, 26), 80, 640, 120);
        // 显示充电动画的 BMP 图像
        if (Is_Touch_In_Area(80, 135, 150, 230) || btn == 0) // 充电监控
        {
            btn = 0;
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电量: ", getColor(0, 41, 250, 26), 80, 250, 150);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, charge_kwh_str, getColor(0, 255, 255, 255), 80, 250, 180);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "kwh", getColor(0, 255, 255, 255), 50, 330, 180);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电费用: ", getColor(0, 41, 250, 26), 80, 460, 150);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, charge_price_str, getColor(0, 255, 255, 255), 80, 460, 180);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "元", getColor(0, 255, 255, 255), 50, 540, 180);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电流: ", getColor(0, 41, 250, 26), 80, 250, 300);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, SLOW_CHARGE_AUS, getColor(0, 255, 255, 255), 80, 250, 330);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "A", getColor(0, 255, 255, 255), 50, 330, 330);

            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电压: ", getColor(0, 41, 250, 26), 80, 460, 300);
            Show_Font(32, 80, 32, 4, getColor(0, 128, 89, 60), 0, 0, SLOW_CHARGE_VUS, getColor(0, 255, 255, 255), 80, 460, 330);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 5, "V", getColor(0, 255, 255, 255), 50, 540, 330);
        }
        if (Is_Touch_In_Area(80, 135, 270, 350) || btn == 1) // 设备信息
        {
            btn = 1;
            Show_Font(22, 100, 22, 4, getColor(0, 128, 89, 60), 0, 0, "粤嵌快充站", getColor(0, 41, 250, 26), 100, 215, 120);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 0, "车位: ", getColor(0, 41, 250, 26), 50, 215, 160);
            Show_Font(22, 50, 22, 4, getColor(0, 128, 89, 60), 0, 0, "10010", getColor(0, 255, 255, 255), 50, 265, 160);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电压: ", getColor(0, 41, 250, 26), 80, 215, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, SLOW_CHARGE_VS, getColor(0, 255, 255, 255), 80, 290, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "充电电流: ", getColor(0, 41, 250, 26), 80, 400, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, SLOW_CHARGE_AS, getColor(0, 255, 255, 255), 80, 475, 200);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "功       率: ", getColor(0, 41, 250, 26), 80, 215, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, SLOW_CHARGE_KW, getColor(0, 255, 255, 255), 80, 290, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "当前电价: ", getColor(0, 41, 250, 26), 80, 400, 240);
            Show_Font(22, 80, 22, 4, getColor(0, 128, 89, 60), 0, 0, "1.2元", getColor(0, 255, 255, 255), 80, 475, 240);
        }
        if (Is_Touch_In_Area(600, 740, 350, 420)) // 充电开关
        {
            is_charging = !is_charging;
            touch_data.ts_x = 0;
            touch_data.ts_y = 0;
            int ret = Show_Choose_Popup("请选择支付方式", "扫码付费", "余额付费");
            if (ret == 1) // 扫码付费
            {
                const char *qr_code_path = Get_Random_QR_Code();
                if (Show_Img_Popup(qr_code_path, "我已支付"))
                {
                    // 页面退出时终止线程
                    is_thread_running = false;         // 停止线程运行
                    pthread_join(charge_thread, NULL); // 等待线程退出
                    Menu_Change(MAIN_PAGE);
                    return;
                }
                else
                    is_charging = !is_charging; // 退出支付则继续充电
            }
            else if (ret == 2) // 余额付费
            {
                if (User_Credit_Sub(charge_price))
                {
                    // 页面退出时终止线程
                    is_thread_running = false;         // 停止线程运行
                    pthread_join(charge_thread, NULL); // 等待线程退出
                    Menu_Change(MAIN_PAGE);
                    return;
                }
                else
                {
                    // 如果返回false，则代表用户余额不够扣除当前费用，则跳转到充值模块，充值后直接扣除费用，并把剩下的余额存到账户中
                    int ret = Show_Choose_Popup("您的余额不足,请充值", "", "确定");
                    if (ret)
                    {
                        // 页面退出时终止线程
                        is_thread_running = false;         // 停止线程运行
                        pthread_join(charge_thread, NULL); // 等待线程退出
                        Menu_Change(MONEY_PAGE);
                        return;
                    }
                }
            }
            printf("充电开关:%d\n", is_charging);
        }
        Lcd_Refresh(lcd_mp, lcd_buf);
    }
    // 页面退出时终止线程
    is_thread_running = false;         // 停止线程运行
    pthread_join(charge_thread, NULL); // 等待线程退出
}

// 选择充电模式界面
void Goto_Charge_Page(void)
{
    Menu_Change(CHARGE_PAGE);
    while (touch_data.page == CHARGE_PAGE)
    {
        Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
        Show_UserInfo();
        Show_Font(50, 100, 50, 4, getColor(0, 255, 158, 64), 10, 0, "快充", getColor(0, 255, 255, 255), 100, 200, 215);
        Show_Font(50, 100, 50, 4, getColor(0, 255, 158, 64), 10, 0, "慢充", getColor(0, 255, 255, 255), 100, 500, 215);
        Lcd_Refresh(lcd_mp, lcd_buf);
        if (Is_Touch_In_Area(200, 300, 215, 265)) // 充电模式
        {
            if (Show_Info_Popup("请插好充电枪", "确定"))
            {
                charge_kwh = 0.0;
                charge_price = 0.0;
                sprintf(charge_kwh_str, "%.2f", charge_kwh); // 保留两位小数
                sprintf(charge_price_str, "%.2f", charge_price);
                sprintf(charge_rate, "%.2f%%", charge_kwh / BATTERY_KWH * 100);
                Menu_Change(FAST_CHARGE_PAGE);
                return;
            }
        }
        if (Is_Touch_In_Area(500, 600, 215, 265)) // 充值缴费
        {
            if (Show_Info_Popup("请插好充电枪", "确定"))
            {
                charge_kwh = 0.0;
                charge_price = 0.0;
                sprintf(charge_kwh_str, "%.2f", charge_kwh); // 保留两位小数
                sprintf(charge_price_str, "%.2f", charge_price);
                sprintf(charge_rate, "%.2f%%", charge_kwh / BATTERY_KWH * 100);
                Menu_Change(SLOW_CHARGE_PAGE);
                return;
            }
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            Menu_Change(MAIN_PAGE);
            return;
        }
    }
}

// 注册登录选择界面
void Goto_Account_Page(void)
{
    Menu_Change(ACCOUNT_PAGE);
    while (touch_data.page == ACCOUNT_PAGE)
    {
        Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
        Show_UserInfo();
        User_Credit_List();
        Lcd_Refresh(lcd_mp, lcd_buf);
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            Menu_Change(MAIN_PAGE);
            return;
        }
    }
}

// 主界面（充电模式、充值缴费、账户查询）
void Goto_Main_Page(void)
{
    Menu_Change(MAIN_PAGE);
    charge_kwh = 0.0;                            // 充电数值
    charge_price = 0.0;                          // 充电价格
    sprintf(charge_kwh_str, "%.2f", charge_kwh); // 保留两位小数
    sprintf(charge_price_str, "%.2f", charge_price);
    sprintf(charge_rate, "%.2f%%", charge_kwh / BATTERY_KWH * 100);
    while (touch_data.page == MAIN_PAGE)
    {
        Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
        Bmp_Show(CHARGE_MODEL, lcd_buf, 50, 100);
        Bmp_Show(MONEY_MODEL, lcd_buf, 300, 100);
        Bmp_Show(ACCOUNT_MODEL, lcd_buf, 550, 100);
        Show_UserInfo();
        Lcd_Refresh(lcd_mp, lcd_buf);
        if (Is_Touch_In_Area(50, 250, 105, 380)) // 充电模式
        {
            Menu_Change(CHARGE_PAGE);
            return;
        }
        if (Is_Touch_In_Area(300, 500, 105, 380)) // 充值缴费
        {
            Menu_Change(MONEY_PAGE);
            return;
        }
        if (Is_Touch_In_Area(550, 750, 105, 380)) // 账户查询
        {
            Menu_Change(ACCOUNT_PAGE);
            return;
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            int ret = Show_Choose_Popup("确定退出车车家充电桩?", "取消", "确认");
            if (ret == 1)
            {
                return;
            }
            Menu_Change(ROLE_PAGE);
            login_user = NULL;
            return;
        }
    }
}

// 用户选择界面
void Goto_User_Page(void)
{
    Menu_Change(MENU_PAGE);
    Bmp_Show(USER_MENU, lcd_buf, 0, 0);
    for (;;)
    {
        Show_UserInfo();
        Lcd_Refresh(lcd_mp, lcd_buf);
        if (Is_Touch_In_Area(0, 800, 220, 360)) // 假设屏幕宽度为 800
        {
            // 判断具体的 X 区域
            if (Is_Touch_In_Area(520, 690, 220, 360)) // 登录按钮区域
            {
                printf("当前在菜单页，跳转到登录页面\n");
                Menu_Change(LOGIN_PAGE);
                return;
            }
            else if (Is_Touch_In_Area(120, 280, 220, 360)) // 注册按钮区域
            {
                printf("当前在菜单页，跳转到注册页面\n");
                Menu_Change(REGISTER_PAGE);
                return;
            }
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            Menu_Change(ROLE_PAGE);
            return;
        }
    }
}

// 注册登录选择界面
void Goto_Role_Page(void)
{
    Menu_Change(ROLE_PAGE);
    Bmp_Show(ROLE_MENU, lcd_buf, 0, 0);
    Lcd_Refresh(lcd_mp, lcd_buf);
    Initialize_Random();
    // 如果触摸点在按钮区域（Y轴：220~360）
    if (Is_Touch_In_Area(0, 800, 220, 360)) // 假设屏幕宽度为 800
    {
        // 判断具体的 X 区域
        if (Is_Touch_In_Area(520, 690, 220, 360)) // 登录按钮区域
        {
            printf("当前在角色选择页，跳转到用户菜单页面\n");
            Menu_Change(MENU_PAGE);
            return;
        }
        else if (Is_Touch_In_Area(120, 280, 220, 360)) // 注册按钮区域
        {
            Show_Info_Popup("暂无功能", "确定");
            return;
        }
    }
}

// 注册界面
void Goto_Register_Page(void)
{
    Menu_Change(REGISTER_PAGE);
    char *account = (char *)calloc(1, 16);  // 最多支持15位字符
    char *passwd = (char *)calloc(1, 8);    // 最多支持7位字符
    char *en_passwd = (char *)calloc(1, 8); // 最多支持7位字符(加密字符)
    bool is_show_passwd = false;
    Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
    Bmp_Show(KEY_BOARD, lcd_buf, 50, 30);

    Show_Font(50, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "注册", getColor(0, 255, 255, 255), 100, 400, 10);
    Show_Font(25, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "请输入账号", getColor(0, 255, 255, 255), 100, 400, 70);
    Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, account, getColor(0, 0, 0, 0), 100, 400, 100);
    Show_Font(25, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "请输入密码", getColor(0, 255, 255, 255), 100, 400, 200);
    Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, passwd, getColor(0, 0, 0, 0), 100, 400, 230);
    Bmp_Show(EN_EYE_BMP, lcd_buf, 700, 200);
    Lcd_Refresh(lcd_mp, lcd_buf);
    while (touch_data.page == REGISTER_PAGE)
    {
        Show_UserInfo();
        if (Is_Touch_In_Area(400, 750, 110, 160)) // 点击了账号输入框
        {
            printf("请输入账号\n");
            Keyboard_Event(account, NULL, false, 11, 400, 100);
        }
        if (Is_Touch_In_Area(400, 750, 220, 280)) // 点击了密码输入框
        {
            printf("请输入密码\n");
            if (Is_Touch_In_Area(700, 750, 220, 280)) // 点击了是否显示密码
            {
                is_show_passwd = !is_show_passwd;
            }
            Keyboard_Event(passwd, en_passwd, is_show_passwd, 6, 400, 230); // 调用键盘事件函数，更新 passwd 数据
        }
        // 更新账号和密码内容到屏幕（保持显示）
        Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, account, getColor(0, 0, 0, 0), 350, 400, 100);
        if (!is_show_passwd)
        {
            Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, en_passwd, getColor(0, 0, 0, 0), 350, 400, 230);
            Bmp_Show(EN_EYE_BMP, lcd_buf, 700, 200);
        }
        else
        {
            Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, passwd, getColor(0, 0, 0, 0), 350, 400, 230);
            Bmp_Show(EYE_BMP, lcd_buf, 700, 200);
        }
        Lcd_Refresh(lcd_mp, lcd_buf);

        if (Is_Touch_In_Area(250, 350, 360, 480)) // 点击了提交按钮
        {
            printf("账号: %s, 密码: %s\n", account, passwd);
            // 校验账号和密码
            int valid = 1; // 标记是否有效
            if (strlen(account) < 11)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "账号需要11位手机号", getColor(0, 0, 0, 255), 350, 400, 150);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 150);
            }
            if (strlen(passwd) < 6)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "密码需要6位数", getColor(0, 0, 0, 255), 350, 400, 280);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 280);
            }

            // 如果账号和密码校验都通过，则显示弹窗
            if (valid)
            {
                User_p user = (User_p)calloc(1, sizeof(User_t));
                strcpy(user->Account, account);
                strcpy(user->Passwd, passwd);
                User_Add(user);
                free(user);
                free(account);
                free(passwd);
                // 绘制弹窗
                Show_Info_Popup("注册成功！", "确认");
                Menu_Change(MENU_PAGE); // 跳转到用户功能界面
                return;
            }
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            free(account);
            free(passwd);
            Menu_Change(MENU_PAGE);
            return;
        }
    }
}

// 登录界面
void Goto_Login_Page(void)
{
    // for (;;)
    // {
    Menu_Change(LOGIN_PAGE);
    char *account = (char *)calloc(1, 16);  // 最多支持15位字符
    char *passwd = (char *)calloc(1, 8);    // 最多支持7位字符
    char *en_passwd = (char *)calloc(1, 8); // 最多支持7位字符(加密字符)
    bool is_show_passwd = false;
    Bmp_Show(BACK_GROUD, lcd_buf, 0, 0);
    Bmp_Show(KEY_BOARD, lcd_buf, 50, 30);
    // Show_Font(50, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "返回", getColor(0, 255, 255, 255), 100, 50, 10);
    Show_Font(50, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "登录", getColor(0, 255, 255, 255), 100, 400, 10);
    Show_Font(25, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "请输入账号", getColor(0, 255, 255, 255), 100, 400, 70);
    Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, account, getColor(0, 0, 0, 0), 100, 400, 100);
    Show_Font(25, 100, 50, 4, getColor(0, 128, 89, 60), 0, 0, "请输入密码", getColor(0, 255, 255, 255), 100, 400, 200);
    Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, passwd, getColor(0, 0, 0, 0), 100, 400, 230);
    Bmp_Show(EN_EYE_BMP, lcd_buf, 700, 200);
    Lcd_Refresh(lcd_mp, lcd_buf);
    while (touch_data.page == LOGIN_PAGE)
    {
        Show_UserInfo();
        if (Is_Touch_In_Area(400, 750, 110, 160)) // 点击了账号输入框
        {
            printf("请输入账号\n");
            Keyboard_Event(account, NULL, false, 11, 400, 100);
        }
        if (Is_Touch_In_Area(400, 750, 220, 280)) // 点击了密码输入框
        {
            printf("请输入密码\n");
            if (Is_Touch_In_Area(700, 750, 220, 280)) // 点击了是否显示密码
            {
                is_show_passwd = !is_show_passwd;
            }
            Keyboard_Event(passwd, en_passwd, is_show_passwd, 6, 400, 230); // 调用键盘事件函数，更新 passwd 数据
        }

        // 更新账号和密码内容到屏幕（保持显示）
        Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, account, getColor(0, 0, 0, 0), 350, 400, 100);
        if (!is_show_passwd)
        {
            Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, en_passwd, getColor(0, 0, 0, 0), 350, 400, 230);
            Bmp_Show(EN_EYE_BMP, lcd_buf, 700, 200);
        }
        else
        {
            Show_Font(25, 350, 50, 4, getColor(0, 255, 255, 255), 0, 12, passwd, getColor(0, 0, 0, 0), 350, 400, 230);
            Bmp_Show(EYE_BMP, lcd_buf, 700, 200);
        }
        Lcd_Refresh(lcd_mp, lcd_buf);

        if (Is_Touch_In_Area(250, 350, 360, 480)) // 点击了提交按钮
        {
            // 校验账号和密码
            int valid = 1; // 标记是否有效
            if (strlen(account) < 11)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "账号需要11位手机号", getColor(0, 0, 0, 255), 350, 400, 150);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 150);
            }
            if (strlen(passwd) < 6)
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "密码需要6位数", getColor(0, 0, 0, 255), 350, 400, 280);
                valid = 0;
            }
            else
            {
                Show_Font(22, 350, 22, 4, getColor(0, 128, 89, 60), 0, 0, "", getColor(0, 0, 0, 0), 350, 400, 280);
            }
            printf("账号: %s, 密码: %s, 合法性%d \n", account, passwd, valid);

            // 如果账号和密码校验都通过，则显示弹窗
            if (valid)
            {
                User_p user = (User_p)calloc(1, sizeof(User_t));
                strcpy(user->Account, account);
                strcpy(user->Passwd, passwd);
                if (User_Check(user))
                {
                    // 登录成功后，不能释放掉这个user，在主函数对login_user进行释放就行
                    printf("登录成功。\n");
                    // 绘制弹窗
                    Show_Info_Popup("登录成功！", "确认");
                    free(user);
                    free(account);
                    free(passwd);
                    free(en_passwd);
                    Menu_Change(MAIN_PAGE);
                    return; // 成功登录后退出函数
                }
                else
                {
                    printf("登录失败。\n");
                    Show_Info_Popup("登录失败！", "确认");
                    free(user);
                    free(account);
                    free(passwd);
                    free(en_passwd);
                    return; // 退出内循环，重新显示页面
                }
            }
        }
        // 点击了返回按钮
        if (Is_Touch_In_Area(20, 100, 0, 50))
        {
            free(account);
            free(passwd);
            free(en_passwd);
            Menu_Change(MENU_PAGE);
            return;
        }
    }
    // }
}

// 菜单触摸选择逻辑
void Menu_Choose(void)
{
    Initialize_Random();
    // 如果触摸点在按钮区域（Y轴：220~360）
    if (Is_Touch_In_Area(0, 800, 220, 360)) // 假设屏幕宽度为 800
    {
        // 判断具体的 X 区域
        if (Is_Touch_In_Area(520, 690, 220, 360)) // 登录按钮区域
        {
            if (touch_data.page == ROLE_PAGE)
            {
                printf("当前在角色选择页，跳转到用户菜单页面\n");
                Goto_User_Page(); // 跳转到登录页面
            }
            else if (touch_data.page == MENU_PAGE)
            {
                printf("当前在菜单页，跳转到登录页面\n");
                Goto_Login_Page(); // 跳转到登录页面
            }
        }
        else if (Is_Touch_In_Area(120, 280, 220, 360)) // 注册按钮区域
        {
            if (touch_data.page == ROLE_PAGE)
            {
                printf("当前在角色选择页，暂无功能\n");
            }
            else if (touch_data.page == MENU_PAGE)
            {
                printf("当前在菜单页，跳转到注册页面\n");
                Goto_Register_Page(); // 跳转到注册页面
            }
        }
    }
}